﻿using Minio.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Himp.Platform
{
    /// <summary>
    /// 标准模型对应用语
    /// </summary>
    public class DataModelPropertyDictService : CrudAppService<DataModelPropertyDict, DataModelPropertyDictDto, Guid, DataModelPropertyDictInput, DataModelPropertyDictCreateDto, DataModelPropertyDictUpdateDto>
    {
        /// <summary>
        /// 
        /// </summary>
        private IRepository<DataModelPropertyDict, Guid> _stdModelAssocPhrDictRepository;
        private DataModelPropertyDictManager _dataModelPropertyDictManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="StdModelConfigRepository"></param>
        public DataModelPropertyDictService(IRepository<DataModelPropertyDict, Guid> stdModelAssocPhrDictRepository
            , DataModelPropertyDictManager dataModelPropertyDictManager) : base(stdModelAssocPhrDictRepository)
        {
            _stdModelAssocPhrDictRepository = stdModelAssocPhrDictRepository;
            _dataModelPropertyDictManager = dataModelPropertyDictManager;
        }

        /// <summary>
        /// 获取标准单词
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<DataModelPropertyDictDto>> GetListAsync(DataModelPropertyDictInput input)
        {
            var iQuery = await _stdModelAssocPhrDictRepository.WithDetailsAsync();
            iQuery = iQuery.Where(i => i.DataModelDictId == input.DataModelDictId);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DataModelPropertyDict>, List<DataModelPropertyDictDto>>(iQuery.ToList());

                return new PagedResultDto<DataModelPropertyDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<DataModelPropertyDict>, List<DataModelPropertyDictDto>>(items.ToList());

                return new PagedResultDto<DataModelPropertyDictDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<DataModelPropertyDictDto> CreateAsync(DataModelPropertyDictCreateDto input)
        {
            var dataModelPropertyDict = await _dataModelPropertyDictManager.Create(
                input.DataModelDictId, input.StdPhrDictId, input.PhrCode, input.PhrName
                , input.LogicalDataType, input.DataLen, input.DataScale, input.PkFlag
                , input.NullableFlag, input.Srtno, input.Exp);

            var itemDto = ObjectMapper.Map<DataModelPropertyDict, DataModelPropertyDictDto>(dataModelPropertyDict);

            return itemDto;
        }
    }
}
